package com.shiwei.is.tools;

import android.content.Context;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Base64;

import com.shiwei.is.ISApplication;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import dji.common.product.Model;
import dji.sdk.base.BaseProduct;
import dji.sdk.sdkmanager.DJISDKManager;
import okio.ByteString;

/**
 * File: null.java
 * Creator: dingpwen
 * Company: shiwei
 * Created on: 2021/3/24 15:20
 * Email: dingpwen@gmail.com
 * Details: TODO
 */
public class Helper {
    private Helper() {
    }

    public static final String WS_SERVER_REMOTE = "ws://124.70.140.183:9527/socket";
    private static final String WS_SERVER_URL = "websocket-address";
    public static final String WS_MSG_TOKEN_SELF = "token";
    private static final String USER_TOKEN_FIX = "wveinwpadlakm@I";
    private static final int USER_TOKEN_POS = 10;

    public static boolean isMavic2Product() {
        BaseProduct baseProduct = ISApplication.getProductInstance();
        if (baseProduct != null) {
            return baseProduct.getModel() == Model.MAVIC_2_PRO || baseProduct.getModel() == Model.MAVIC_2_ZOOM;
        }
        return false;
    }

    public static boolean isM300Product() {
        if (DJISDKManager.getInstance().getProduct() == null) {
            return false;
        }
        Model model = DJISDKManager.getInstance().getProduct().getModel();
        return model == Model.MATRICE_300_RTK;
    }

    public static void setWebSocketUrl(Context context, final String url) {
        PreferenceManager.getDefaultSharedPreferences(context).edit()
                .putString(WS_SERVER_URL, url)
                .apply();
    }

    public static String getWebSocketUrl(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context)
                .getString(WS_SERVER_URL, WS_SERVER_REMOTE);
    }

    private static String generateUserToken(String pwd){
        final String token = "" + System.currentTimeMillis();
        String md5Token = null;
        if(pwd.isEmpty()) {
            pwd = USER_TOKEN_FIX;
        }
        try{
            MessageDigest md = MessageDigest.getInstance("md5");
            byte[] md5 = md.digest(token.getBytes(StandardCharsets.UTF_8));
            byte[] fix = md.digest(pwd.getBytes(StandardCharsets.UTF_8));
            byte[] data = new byte[md5.length + fix.length];
            System.arraycopy(md5, 0, data, 0, USER_TOKEN_POS);
            System.arraycopy(fix, 0, data, USER_TOKEN_POS, fix.length);
            System.arraycopy(md5, USER_TOKEN_POS, data, fix.length + USER_TOKEN_POS, md5.length - USER_TOKEN_POS);
            md5Token = Base64.encodeToString(data, 0);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return md5Token;
    }

    public static void saveUserToken(Context context, final String pwd) {
        String token = generateUserToken(pwd);
        PreferenceManager.getDefaultSharedPreferences(context).edit()
                .putString(WS_MSG_TOKEN_SELF, token)
                .apply();
    }

    public static String getUserToken(Context context, Boolean generate) {
        String token = PreferenceManager.getDefaultSharedPreferences(context)
                .getString(WS_MSG_TOKEN_SELF, "");
        if(generate && TextUtils.isEmpty(token)) {
            token = generateUserToken("");
        }
        return token;
    }

    public static ByteString addToSizeByteArray(byte[] data, int size){
        if(size > data.length || ((size >> 24) != 0 )) {
            throw new Error("Invalid data!size=" + size + ";data.length" + data.length);
        }
        byte[] dstData = new byte[size + 3];
        dstData[2] = (byte)(size & 0x00ff);
        dstData[1] = (byte)((size >> 8) & 0x00ff);
        dstData[0] = (byte)((size >> 16) & 0x00ff);
        System.arraycopy(data, 0, dstData, 3, size);
        return new ByteString(dstData);
    }
}
